package com.example.testapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Radio extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private TextView tv_sex; // 声明一个文本视图对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio);
        tv_sex = findViewById(R.id.tv_sex);
        RadioGroup rg_sex = findViewById(R.id.rg_sex);

        rg_sex.setOnCheckedChangeListener(this);

       // rg_sex.setOnCheckedChangeListener(new RadioListener());
    }

    // 在用户点击组内的单选按钮时触发
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        if(i == R.id.rb_male){
            tv_sex.setText("哇哦，你是个帅气的男孩");
        } else if (i == R.id.rb_female) {
            tv_sex.setText("哇哦，你是个漂亮的女孩");
        }
    }
    // 定义一个单选监听器，它实现了接口RadioGroup.OnCheckedChangeListener
    private class RadioListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            Toast.makeText(Radio.this,  "您选中了控件"+i, Toast.LENGTH_SHORT).show();
        }
    }
}